﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using EpiNEXT.Core;

namespace EpiNEXT.WPF.FormDesigner.Pages
{
    /// <summary>
    /// A class designed to be the view model for a HOME page.
    /// </summary>
    public class HomeViewModel : ObservableObject
    {
        #region Members
        public ObservableCollection<ProjectInfo> _projectsInfo = new ObservableCollection<ProjectInfo>();
        #endregion // Members

        #region Properties
        public ObservableCollection<ProjectInfo> ProjectsInfo
        {
            get
            {
                return this._projectsInfo;
            }
            private set
            {
                if (this._projectsInfo != value)
                {
                    this._projectsInfo = value;
                    RaisePropertyChanged("ProjectsInfo");
                }
            }
        }
        #endregion // Properties

        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        public HomeViewModel()
        {
            PopulateProjectList();
        }
        #endregion // Constructors

        #region Methods
        /// <summary>
        /// Populates the list of projects
        /// </summary>
        private void PopulateProjectList()
        {
            ProjectsInfo = new ObservableCollection<ProjectInfo>();

            string[] filePaths = Directory.GetFiles(@"C:\Users\bioha_000\Documents\Visual Studio 2013\Projects\EpiNEXT\EpiNEXT.WPF.View\bin\Debug\Projects\", "*.prj");
            //string[] filePaths = Directory.GetFiles(@"C:\Users\knu1\Source\Workspaces\epinext\EpiNEXT\EpiNEXT.WPF.View\bin\Debug\Projects", "*.prj");
            
            if (filePaths.Length > 0)
            {
                foreach (string filePath in filePaths)
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(filePath);

                    XmlNode description = xmlDoc.SelectSingleNode("/Project/Description");
                    XmlNode summary = xmlDoc.SelectSingleNode("/Project/Summary");
                    XmlNode author = xmlDoc.SelectSingleNode("/Project/Author");
                    XmlNode created = xmlDoc.SelectSingleNode("/Project/Created");
                    XmlNode lastSaved = xmlDoc.SelectSingleNode("/Project/LastSaved");

                    if (lastSaved == null || created == null) continue;

                    XmlNode project = xmlDoc.SelectSingleNode("/Project");
                    ProjectInfo projectInfo = new ProjectInfo();
                    projectInfo.Title = project.Attributes["Title"].Value.ToString();
                    projectInfo.Description = description.InnerText;
                    projectInfo.Summary = summary.InnerText;
                    projectInfo.Author = author.InnerText;

                    long saved;
                    bool success = long.TryParse(lastSaved.InnerText, out saved);
                    if(success) 
                    {
                        projectInfo.LastSaved = new DateTime(saved);
                    }

                    projectInfo.FileInfo = new FileInfo(filePath);

                    // load forms
                    XmlNodeList nodes = xmlDoc.SelectNodes(@"/Project/Forms/Form");

                    foreach (XmlNode node in nodes)
                    {
                        FormInfo formInfo = new FormInfo();
                        
                        Guid formId = new Guid(node.Attributes["Id"].Value.ToString());
                        string formTitle = node.Attributes["Title"].Value.ToString();
                        bool isRelatedForm = bool.Parse(node.Attributes["IsRelatedForm"].Value.ToString());

                        if (isRelatedForm) continue;

                        formInfo.Title = formTitle;
                        formInfo.Id = formId;

                        XmlNode formLastSaved = node.SelectSingleNode("LastSaved");
                        XmlNode formDescription = node.SelectSingleNode("Description");

                        success = long.TryParse(formLastSaved.InnerText, out saved);
                        if (success)
                        {
                            formInfo.LastSaved = new DateTime(saved);
                        }

                        formInfo.Description = formDescription.Value;
                        formInfo.ProjectInfo = projectInfo;

                        projectInfo.Forms.Add(formInfo);
                    }

                    ProjectsInfo.Add(projectInfo);
                }
            }
        }
        #endregion // Methods
    }
}
